<?php
/**
 * Description: 预支付
 * User: devforma
 * Date: 15/6/2
 * Time: 14:29
 */
require CONFIG_DIR . '/pay.config.php';

class PreparePay implements iController {
    private $queryArgs;

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        if (validateUser($queryArgs) !== 1 || empty($queryArgs['order_num']) || empty($queryArgs['platform']))
            return genOutput(1000);

        //获取订单信息
        $order = getOrderInfo($queryArgs['order_num'], $queryArgs['user_id']);

        if (empty($order))
            return genOutput(1000);

        //订单是否过期
        if (!empty($order->invalid_time) && $order->invalid_time <= date('Y-m-d H:i:s')) {
            return genOutput(8003);
        }

        $this->queryArgs = $queryArgs;

        //获取商品信息
        $productInfo = $this->getProductInfo($order->product_id, $order->product_type);

        //获取具体支付对象
        $payment = $this->getPayment($queryArgs['platform']);

        if (empty($payment))
            return genOutput(1001);

        //获取预支付信息
        if (is_a($payment, 'Alipay')) {
            if ($order->client == 'mobile_web') {
                $data = $payment->wapPay($order->order_id, $productInfo['name'], $order->price);
            } elseif ($queryArgs['platform'] == 'bank') {
                $data = $payment->bankPay($order->order_id, $productInfo['name'], $order->price, $productInfo['valid_pay_period']);
            } else {
                $data = $payment->pay($order->order_id, $productInfo['name'], $order->price, $productInfo['valid_pay_period']);
            }
        } else {
            $data = null;
        }

        return genOutput(1, array(
            'info' => $data
        ));
    }

    /**
     * 获取商品信息
     * @param int $productId 商品id
     * @param string $productType 商品类型
     * @return array
     */
    private function getProductInfo($productId, $productType) {
        global $cmsdb;

        $info = array('name' => '', 'valid_pay_period' => 30);

        switch ($productType) {
            case 'zhiboke':
                $data = getOrderCourseInfo($productId);

                $info['name'] = $data->course_name;
                if ($data->valid_pay_period)
                    $info['valid_pay_period'] = $data->valid_pay_period;

                break;

            case 'course':
                $sql = 'SELECT `course_name` FROM `data_vod_course` WHERE `id`=:id';
                $info['name'] = $cmsdb->getVar($sql, array(':id' => $productId));

                break;

            case 'interview':
                $sql = 'SELECT `product_name` FROM `conf_product` WHERE `id`=:id';
                $info['name'] = $cmsdb->getVar($sql, array(':id' => $productId));

                break;

            default:
                break;
        }

        $info['name'] = str_replace('+', '和', $info['name']);

        return $info;
    }


    /**
     * 获取支付对象
     * @param string $platform 支付平台
     * @return null | Object
     */
    private function getPayment($platform) {
        if ($platform == 'bank' && empty($this->queryArgs['bank'])) return null;

        $payment = null;
        if ($platform == 'alipay' || $platform == 'bank') {
            include __DIR__ . '/includes/Alipay.class.php';
            $payment = new Alipay();

            if ($platform == 'bank') $payment->setBank($this->queryArgs['bank']);

        } else if ($platform == 'weixin') {
            include __DIR__ . '/includes/WeixinPay.class.php';
            $payment = new WeixinPay();
        }

        return $payment;
    }

}